package com.ms.education.course.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.JwtUtils;
import com.ms.education.R;
import com.ms.education.course.entity.ClassUser;
import com.ms.education.course.entity.Classroom;
import com.ms.education.course.entity.vo.FrontClassInfoVo;
import com.ms.education.course.entity.vo.PageClassVo;
import com.ms.education.course.service.ClassUserService;
import com.ms.education.course.service.ClassroomService;
import com.ms.education.exceptionHandler.EduException;
import com.netflix.ribbon.proxy.annotation.Http;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>Project:education-IndexClassController
 * <p>Powered by ms on 2023-03-04 15:59:35
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@RestController
@RequestMapping("/course/front/class")
public class IndexClassController {

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private ClassUserService classUserService;

    @GetMapping("pageClass/{current}/{limit}")
    public R pageClass(@PathVariable("current") long current,
                       @PathVariable("limit") long limit,
                       @RequestParam(required = false) String name) {
        Page<Classroom> page = new Page<>(current, limit);
        QueryWrapper<Classroom> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "Normal");
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        classroomService.page(page, wrapper);
        List<Classroom> classrooms = page.getRecords();
        long rows = page.getTotal();
        return R.ok().data("rows", rows).data("classrooms", classrooms);
    }

    @GetMapping("getFrontClassInfo/{id}")
    public R getFrontClassInfo(@PathVariable("id") Integer id) {
        FrontClassInfoVo classroom = classroomService.getFrontClassInfo(id);
        return R.ok().data("classroom", classroom);
    }

    @ApiOperation("判断用户是否加入课程")
    @GetMapping("isUserInClass/{classId}")
    public R isUserInClass(@PathVariable("classId") Integer classId) {
        boolean isIn = classUserService.isUserInClass(classId, Convert.toInt(StpUtil.getLoginId()));
        return R.ok().data("isIn", isIn);
    }

    @ApiOperation("用户加入班级")
    @PutMapping("userInClass/{classId}")
    public R userInClass(@PathVariable("classId") Integer classId) {
        ClassUser classUser = new ClassUser();
        classUser.setUserId(Convert.toInt(StpUtil.getLoginId()));
        classUser.setClassId(classId);
        classUserService.save(classUser);
        return R.ok();
    }


    @ApiOperation("用户退出班级")
    @DeleteMapping("userOutClass/{classId}")
    public R userOutClass(@PathVariable("classId") Integer classId) {
        UpdateWrapper<ClassUser> wrapper = new UpdateWrapper<ClassUser>().eq("user_id", Convert.toInt(StpUtil.getLoginId())).eq("class_id", classId);
        classUserService.remove(wrapper);
        return R.ok();
    }

    @SaCheckLogin
    @ApiOperation("查询用户加入的班级")
    @GetMapping("getUserClass/{current}/{limit}")
    public R getUserClass(@PathVariable("current") long current,
                          @PathVariable("limit") long limit) {
        PageClassVo pageClassVo = new PageClassVo();
        pageClassVo.setUserId(Convert.toInt(StpUtil.getLoginId()));
        Page<Classroom> page = classroomService.pageClass(current, limit, pageClassVo);
        return R.ok().data("classrooms", page.getRecords()).data("rows", page.getTotal());
    }


}
